package com.dpf.hou.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dpf.hou.mapper.UserMapper;
import com.dpf.hou.pojo.User;
import com.dpf.hou.service.UserService;
import com.dpf.hou.util.Base64Util;
import com.dpf.hou.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAllUser() {
        List<User> list=userMapper.selectList(null);
        return list;
    }

    @Override
    public ResultUtil loginUser(String username, String password) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("password",password);
        User user=userMapper.selectOne(queryWrapper);
        ResultUtil result=null;

        if(user==null){
            result=new ResultUtil(0,"账号或密码错误",null);
        }else {
            if(user.getStatus()==1){
                result=new ResultUtil(1,"该账号已被封禁",null);
            }else {
                if(user.getType()==1){
                    result=new ResultUtil(2,"管理员登录成功",user);
                }else {
                    result=new ResultUtil(3,"用户登录成功",user);
                }
            }
        }
        return result;
    }

    @Override
    public boolean addUser(User user) {

        QueryWrapper<User> q=new QueryWrapper<>();
        q.eq("username",user.getUsername());
        if(userMapper.selectOne(q)!=null){
            return false;
        }
        int rows=userMapper.insert(user);
        return rows == 0 ? false:true;
    }

    @Override
    public boolean updateUser(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",user.getUserid());
        int rows=userMapper.update(user,queryWrapper);
        return rows == 0? false:true;
    }

    @Override
    public Page<User> getPage(int page, int size,int[] userids) {
        Page<User> p=new Page<>(page,size);
        for(int i=0;i<userids.length;i++){
            System.out.println(userMapper.selectUserid(userids[i]));
        }
        return p;
    }

    @Override
    public Page<User> getPageByName(int page, int size, String username) {
        QueryWrapper<User> q=new QueryWrapper<>();
        if(username!=null && !"".equals(username)){
            q.like("username",username);
        }
        Page<User> p=new Page<>(page,size);
        return userMapper.selectPage(p,q);
    }

    @Override
    public boolean delBatch(List<Integer> userid) {

        int n=userid.size();
        if(n==0){
            return false;
        }
        for (int i=0;i<n;i++){
            userMapper.deleteById(userid.get(i));
        }
        return true;
    }

    @Override
    public List<User> list() {
        return userMapper.selectList(null);
    }

    @Override
    public void saveBatch(List<User> list){
        for (int i=0;i<list.size();i++){
            addUser(list.get(i));
        }
    }

    @Override
    public boolean pwdChange(User user) {
        User u=new User();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",user.getUserid());
        u=userMapper.selectOne(queryWrapper);
        String encodePassword = Base64Util.base64Encode(user.getPassword());
        u.setPassword(encodePassword);
        int rows=userMapper.update(u,queryWrapper);
        return rows == 0? false:true;
    }

    @Override
    public String getEncode(String password) {
        String encodePassword = Base64Util.base64Encode(password);
        return encodePassword;
    }
}
